//
//  HDSchoolViewController.swift
//  Excited
//
//  Created by X140Yu on 6/13/16.
//  Copyright © 2016 X140Yu. All rights reserved.
//

import UIKit
import PageMenu
import RxCocoa
import RxSwift
import DropDown

class HDSchoolViewController: UIViewController {
    let disposeBag = DisposeBag()
    var pageMenu : CAPSPageMenu?
//    let dropDownView = UIButton()
//    let dropDownMenu = DropDown()
    var classTableRefreshBarButton: UIBarButtonItem?
//    var classTableController: HDClassTableViewController?
    var gradeController: HDGradeViewController?
//    var examController: HDExamViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(userDidLogin), name: HDUserDidLoginNotificationKey, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(userDidLogout), name: HDUserDidLogoutNotificationKey, object: nil)
        
//        classTableRefreshBarButton = UIBarButtonItem(image: UIImage(named: "hd-refresh")?.imageWithRenderingMode(.AlwaysOriginal), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(refreshClassTable))

        var controllerArray : [UIViewController] = []

        
//        classTableController = HDClassTableViewController()
//        classTableController!.title = "课表"
//        controllerArray.append(classTableController!)

        gradeController = HDGradeViewController()
        gradeController!.title = "成绩"
        controllerArray.append(gradeController!)
        
//        examController = HDExamViewController()
//        examController!.title = "考试"
//        controllerArray.append(examController!)

        let parameters: [CAPSPageMenuOption] = [
            .MenuItemSeparatorWidth(4.3),
            .UseMenuLikeSegmentedControl(true),
            .MenuItemSeparatorPercentageHeight(0.1),
            .ScrollMenuBackgroundColor(UIColor.redColor()),
            .UnselectedMenuItemLabelColor(UIColor.whiteColor()),
            .MenuItemSeparatorColor(UIColor.clearColor())
        ]
        
        pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 64, view.frame.width, view.frame.height - 64.0 - 64.0 + 15), pageMenuOptions: parameters)
        pageMenu?.delegate = self
        view.addSubview(pageMenu!.view)

//        let dataSource = ["第一周", "第二周", "第三周", "第四周", "第五周", "第六周", "第七周", "第八周", "第九周", "第十周", "第十一周", "第十二周", "第十三周", "第十四周", "第十五周", "第十六周", "第十七周", "第十八周"]
//        dropDownMenu.dataSource = dataSource
//        dropDownMenu.anchorView = dropDownView
//        dropDownMenu.bottomOffset = CGPoint(x: 0, y:30)
//
//        dropDownMenu.selectionAction = { [unowned self] (index: Int, item: String) in
//            self.dropDownView.setTitle(item, forState: .Normal)
//            self.classTableController!.setCurrentWeek(index+1)
//            HDCache.saveCurrentWeek(index + 1)
//        }
//
//        dropDownView.frame.size.height = 30
//        dropDownView.frame.size.width = 120
//        dropDownView.setTitle(dataSource[HDCache.getCurrentWeek() - 1], forState: UIControlState.Normal)
//        dropDownView.setTitleColor(UIColor.whiteColor(), forState: .Normal)
//        
//        dropDownView.setImage(UIImage(named: "hd-dropdown"), forState: UIControlState.Normal)
//
//        navigationItem.titleView = dropDownView
//        dropDownView.rx_tap.subscribeNext { [unowned self] in
//            self.dropDownMenu.show()
//        }.addDisposableTo(disposeBag)
//        
//        
//        navigationItem.rightBarButtonItem = classTableRefreshBarButton
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func userDidLogin() {
        navigationItem.titleView?.hidden = false
        navigationItem.rightBarButtonItem = classTableRefreshBarButton
    }
    
    func userDidLogout() {
        navigationItem.titleView?.hidden = true
        navigationItem.rightBarButtonItem = nil
    }
   
//    func refreshClassTable() {
//        classTableController?.refreshClassTable()
//    }
//
//    override func viewWillAppear(animated: Bool) {
//        super.viewWillAppear(animated)
//        
//        if let user = HDCache.getCurrentLoginUser() {
//            if user.shouldRefreshLogin() == true {
//                refreshLogin()
//            }
//            if classTableController?.classInfoArray.count == 0 {
//                classTableController?.refreshClassTable()
//            }
//        } else {
//            classTableController?.classInfoArray.removeAll()
//            classTableController?.classTableView.setClass([HDClass](), week: 1)
//            gradeController?.scoreArray.removeAll()
//            gradeController?.tableView.reloadData()
//            examController?.examArray.removeAll()
//            examController?.tableView.reloadData()
//            navigationItem.titleView?.hidden = true
//            navigationItem.rightBarButtonItem = nil
//        }
//    }
}

extension HDSchoolViewController {
    
}


extension HDSchoolViewController: CAPSPageMenuDelegate {
    func didMoveToPage(controller: UIViewController, index: Int) {
//        if HDCache.getCurrentLoginUser() == nil {
//            return
//        }
//        if index == 0 {
//            navigationItem.titleView?.hidden = false
//            navigationItem.rightBarButtonItem = classTableRefreshBarButton
//        } else {
//            navigationItem.titleView?.hidden = true
//            navigationItem.rightBarButtonItem = nil
//        }
    }
}